import { merge } from "lodash-es";
import { mapStore } from "../utils/store";
import { $http } from "../boot/http";

export default merge(
  {
    namespaced: true,
    state: {
      payPasswordSetShow: null,
      realSetShow: null,
      accessToken: null,
      userCertify: null
    },
    mutations: {
      setPayPasswordSetShow(state, show) {
        state.payPasswordSetShow = show;
      },
      setRealSetShow(state, show) {
        stat.realSetShow = show;
      },
      setAccessToken(state, accessToken) {
        state.accessToken = accessToken;
      },
      setUserCertify(state, userCertify) {
        state.userCertify = userCertify;
      }
    },
    getters: {
      hasLogined(state) {
        return state.user !== null;
      }
    },
    actions: {
      notifyMobile({ dispatch }, { mobile, type }) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.notify.mobile",
            loading: async () => {
              const { data } = await $http.post("/api/v1/notify/mobile", {
                mobile,
                type
              });

              return data;
            }
          },
          { root: true }
        );
      },
      notifyEmail({ dispatch }, { email, type }) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.notify.email",
            loading: async () => {
              const { data } = await $http.post("/api/v1/notify/email", {
                email,
                type
              });

              return data;
            }
          },
          { root: true }
        );
      },
      logout({ commit }) {
        commit("clean", null, { root: true });
      },
      async login({ commit, dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.login",
            loading: async () => {
              const { data } = await $http.post("/api/v1/login", params);
              commit("setAccessToken", data.access_token);
              await dispatch("loadUser");
            }
          },
          { root: true }
        );
      },
      async mobileLogin({ commit, dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.mobile.login",
            loading: async () => {
              const { data } = await $http.post("/api/v1/mobile_login", params);
              commit("setAccessToken", data.access_token);

              return dispatch("loadUser");
            }
          },
          { root: true }
        );
      },
      async register({ commit, dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.register",
            loading: async () => {
              const { data } = await $http.post("/api/v1/register", params);
              return data;
            }
          },
          { root: true }
        );
      },
      async mobileRegister({ commit, dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.mobile.register",
            loading: async () => {
              const { data } = await $http.post(
                "/m/mfc/api/reg",
                params
              );
              return data;
            }
          },
          { root: true }
        );
      },
      async emailRegister({ commit, dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.email.register",
            loading: async () => {
              const { data } = await $http.post(
                "/api/v1/email_register",
                params
              );
              return data;
            }
          },
          { root: true }
        );
      },
      async setPayPassword({ dispatch }, { password }) {
        await dispatch(
          "toggleLoading",
          {
            key: "user.reset_password_by_code",
            loading: async () =>
              await $http.post("/api/v1/auth/pay_password", { password })
          },
          { root: true }
        );
        dispatch("loadUser", { options: { force: true } });
      },
      async resetPassword({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.reset_password_by_code",
            loading: async () =>
              await $http.post("/api/v1/reset/password", params)
          },
          { root: true }
        );
      },
      async resetPasswordByEmail({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.reset_password_by_code",
            loading: async () =>
              await $http.post("/api/v1/reset/email_password", params)
          },
          { root: true }
        );
      },
      async changePassword({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.reset_password_by_old",
            loading: async () =>
              await $http.post("/api/v1/auth/reset/password_by_old", params)
          },
          { root: true }
        );
      },
      async changePayPasswordByOldPassword({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.changePayPassword",
            loading: async () =>
              await $http.post("/api/v1/auth/reset/pay_password_by_old", params)
          },
          { root: true }
        );
      },
      async changePayPasswordBySms({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.changePayPassword",
            loading: async () => {
              await $http.post("/api/v1/auth/reset/pay_password", params);
            }
          },
          { root: true }
        );
      },
      async changePayPasswordByEmail({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.changePayPassword",
            loading: async () => {
              await $http.post(
                "/api/v1/auth/reset/pay_password_by_email",
                params
              );
            }
          },
          { root: true }
        );
      },
      async changeMobile({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.changeMobile",
            loading: async () => {
              await $http.post("/api/v1/auth/reset/mobile_by_old", params);
            }
          },
          { root: true }
        );
      },
      async changeEmail({ dispatch }, params) {
        return dispatch(
          "toggleLoading",
          {
            key: "user.changeEmail",
            loading: async () => {
              await $http.post("/api/v1/auth/reset/email_by_old", params);
            }
          },
          { root: true }
        );
      },
      async saveUserCertify({ commit, dispatch }, params) {
        const { data } = await dispatch(
          "toggleLoading",
          {
            key: "user.certify.post",
            loading: () => $http.post("/api/v1/certify", params)
          },
          { root: true }
        );
        commit("setUserCertify", data);
        return data;
      },
      async loadUserCertify(
        { commit, dispatch, state },
        { options = {}, ...params } = {}
      ) {
        if (!state.userCertify || options.force) {
          const { data } = await dispatch(
            "toggleLoading",
            {
              key: "user.certify",
              loading: () => $http.get("/api/v1/certify", params)
            },
            { root: true }
          );

          commit("setUserCertify", data);
          return data;
        }

        return state.userCertify;
      },
      async saveUserBank(
        { commit, dispatch, state },
        { options = {}, ...params } = {}
      ) {
        const data = await dispatch(
          "toggleLoading",
          {
            key: "user.bank.save",
            loading: () => $http.post("/api/v1/bank/save", params)
          },
          { root: true }
        );
        dispatch("loadBanks");
        return data;
      },
      async enableUserBank(
        { commit, dispatch, state },
        { options = {}, ...params } = {}
      ) {
        const data = await dispatch(
          "toggleLoading",
          {
            key: "user.bank.enable",
            loading: () => $http.post("/api/v1/bank/enable", params)
          },
          { root: true }
        );
        dispatch("loadBanks");
        return data;
      },
      async deleteUserBank(
        { commit, dispatch, state },
        { options = {}, ...params } = {}
      ) {
        const data = await dispatch(
          "toggleLoading",
          {
            key: "user.bank.delete",
            loading: () => $http.post("/api/v1/bank/delete", params)
          },
          { root: true }
        );
        dispatch("loadBanks");
        return data;
      }
    }
  },
  mapStore("user", {
    url: " api/v1/auth/info ",
    getter: false,
    options: {
      actionOptions: { commitType: "data", cacheState: true }
    }
  }),
  mapStore("banks", {
    url: "api/v1/bank",
    options: {
      actionOptions: { commitType: "data" }
    },
    loadingKey: "user.banks"
  })
);
